package com.flats.webview;


import com.flats.model.entity.Users;
import com.flats.service.transactional.TransactionalUserService;

import com.flats.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    TransactionalUserService userService;


    @RequestMapping(value = "/login.form"/*,method= RequestMethod.GET*/)
    @ResponseBody
    public String  login(Model model, @RequestParam(value = "name") String name, @RequestParam(value = "email") String email
            , @RequestParam(value = "password") String password, @RequestParam(value = "phone") String phone) {
        System.out.println("LOGIN");

        List<String> result=new ArrayList<String>(2);
        String returnText = "You successfully registered.";
        if (StringUtils.isBlank(name)) {
            returnText = "Name field is empty.";
        } else if (StringUtils.isBlank(email)) {
            returnText = "Email field is empty.";
        } else if (StringUtils.isBlank(password)) {
            returnText = "Password field is empty.";
        } else if (StringUtils.isBlank(phone)) {
            returnText = "Phone field is empty.";
        } else{
//            Cabinet cabinet=new Cabinet();
//            Long cabinetId = (Long) cabinetService.saveOrUpdate(cabinet);
//            System.out.println("cabinetId = " + cabinetId);
//            cabinet = cabinetService.get(cabinetId.longValue());
            Users user = new Users();
            user.setName(name);
            user.setEmail(email);
            user.setTelephone(phone);
            user.setPassword(password);
//            user.setCabinet(cabinet);
            Long userId = (Long) userService.saveOrUpdate(user);
            System.out.println("SUCCESS SAVE!!!!!!!!!!!");
            Users userCreated = userService.get(userId.longValue());
            Utils.setSession(Constants.USER, userCreated);
            Users userFromSess = (Users)Utils.getSession(Constants.USER);
            System.out.println("userFromSess.getId() = " + userFromSess.getId());
            System.out.println("userFromSess.getName = " + userFromSess.getName());
            returnText=returnText+userFromSess.getName();
            System.out.println("returnText = " + returnText);
        }
        return returnText;

    }
    @RequestMapping(value = "/logout")
    @ResponseBody
    public void logout(){
        Utils.setSession(Constants.USER, null);
    }
    @RequestMapping(value = "/login.inline")
    @ResponseBody
    public String login(@RequestParam(value = "email") String email
            , @RequestParam(value = "password") String password){
        for (Users users : userService.getAll()) {
            if(password.equals(users.getPassword())){
                Utils.setSession(Constants.USER, users);
                return users.getName();
            }
        }
        return null;
    }

}
